import {
  createRouter,
  createWebHashHistory
} from 'vue-router'
const routes = [{
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    meta: {
      title: '登录'
    },
    component: () => import('../view/Login.vue')
  },
  {
    path: '/home',
    name: '主页',
    meta: {
      title: '主页'
    },
    component: () => import('../view/Home.vue'),
    redirect: '/index',
    children: [{
        path: '/index',
        meta: {
          title: '首页'
        },
        component: () => import('../view/Welcome.vue')
      },
      
      {
        path: '/week_report',
        meta: {
          title: '工作简报',
          requiresAuth: true,
          requiredRoles: [8] // 用户需要角色为9或以上才能访问该路由
        },
        component: () => import('../view/week_report/Index.vue'),
      },
      {
        path: '/week_report/add',
        meta: {
          title: '新增',
          requiresAuth: true,
          requiredRoles: [8] // 用户需要角色为9或以上才能访问该路由
        },
        component: () => import('../view/week_report/Add.vue'),
      },
      {
        path: '/member_report',
        meta: {
          title: '人员情况表',
          requiresAuth: true,
          requiredRoles: [8] // 用户需要角色为8或以上才能访问该路由
        },
        component: () => import('../view/member_report/Index.vue'),
      },
      {
        path: '/member_report/table',
        meta: {
          title: '人员情况表',
          requiresAuth: true,
          requiredRoles: [8] // 用户需要角色为8或以上才能访问该路由
        },
        component: () => import('../view/member_report/Table.vue'),
      },
      {
        path: '/work_mine',
        meta: {
          title: '我的项目'
        },
        component: () => import('../view/work_mine/Index.vue'),
      }, 
      {
        path: '/work_score',
        meta: {
          title: '项目工分',
          requiresAuth: true,
          requiredRoles: [10] // 用户需要角色为10或以上才能访问该路由
        },
        component: () => import('../view/work_score/Index.vue'),
      }, 
      {
        path: '/work_score/person',
        meta: {
          title: '人员工分',
          requiresAuth: true,
          requiredRoles: [10] // 用户需要角色为10或以上才能访问该路由
        },
        component: () => import('../view/work_score/Person.vue'),
      }, 
      {
        path: '/work_score/good',
        meta: {
          title: '好稿列表',
          requiresAuth: true,
          requiredRoles: [10] // 用户需要角色为10或以上才能访问该路由
        },
        component: () => import('../view/work_score/Good.vue'),
      },
      {
        path: '/work_file',
        meta: {
          title: '项目文件'
        },
        component: () => import('../view/work_file/Index.vue'),
      }, 
      {
        path: '/work_list',
        meta: {
          title: '项目列表'
        },
        component: () => import('../view/work_list/Index.vue'),
      },
      {
        path: '/work_count',
        meta: {
          title: '项目统计'
        },
        component: () => import('../view/work_count/Index.vue'),
      }, 
      {
        path: '/paper_recommend',
        meta: {
          title: '好稿自荐'
        },
        component: () => import('../view/good_paper/Recommend.vue'),
      }, 
      {
        path: '/paper_vote',
        meta: {
          title: '好稿评选'
        },
        component: () => import('../view/good_paper/Vote.vue'),
      },
      {
        path: '/paper_result',
        meta: {
          title: '好稿结果'
        },
        component: () => import('../view/good_paper/Result.vue'),
      },

      {
        path: '/homework_list',
        meta: {
          title: '作业列表'
        },
        component: () => import('../view/homework/Index.vue'),
      }, 
      {
        path: '/homework_list/upload',
        meta: {
          title: '作业上传'
        },
        component: () => import('../view/homework/Upload.vue'),
      }, 
      {
        path: '/user/list',
        meta: {
          title: '用户管理'
        },
        component: () => import('../view/user/Index.vue'),
      },
      {
        path: '/user/detail',
        meta: {
          title: '用户详情'
        },
        component: () => import('../view/user/Detail.vue'),
      },
      {
        path: '/unauthorized',
        name: 'Unauthorized',
        component: () => import('../view/Unauthorized.vue')
      },
      {
        path: '/other/list',
        meta: {
          title: '随便看看'
        },
        component: () => import('../view/other/Index.vue'),
      }, {
        path: '/other/add',
        meta: {
          title: '新增信息'
        },
        component: () => import('../view/other/Add.vue'),
      }
    ]
  },
]
const router = createRouter({
  history: createWebHashHistory(),
  routes
})


// 路由导航守卫
router.beforeEach((to, from, next) => {
  // 修改页面 title
  if (to.meta.title) {
    document.title = `创意设计中心工单管理 - ${to.meta.title}`;
  } else {
    document.title = '创意设计中心工单管理';
  }

  // 放行登录页面
  if (to.path === '/login') {
    localStorage.clear();
    sessionStorage.clear();
    return next();
  }

  // 获取username
  const username = sessionStorage.getItem('username');
  if (!username) {
    return next('/login');
  } else {
    // 检查路由是否需要权限
    if (to.meta.requiresAuth) {
      const requiredRoles = to.meta.requiredRoles || [];
      const userRole = parseInt(sessionStorage.getItem('role'));

      const hasPermission = requiredRoles.length === 0 || requiredRoles.some(role => userRole >= role);

      if (!hasPermission) {
        return next('/unauthorized'); // 没有权限，重定向到未授权页面
      }
    }
    next(); // 认证且有权限，允许访问
  }
});

// 导出路由
export default router